package cn.kgc.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author wangyue
 * @version 1.0
 */
@Slf4j
@Component
@EnableRetry
public class SmsSenderRetry {
    @Resource
    private SmsSender smsSender;

    @Retryable(
            //如果出现Exception异常进行重试
            value = {Exception.class},
            //最大重试次数
            maxAttempts = 2,
            //重试间隔时间 每次间隔时间是上一次的2倍
            backoff = @Backoff(delay = 1000,multiplier = 2)
    )
    public void sendSms(String phone,String code){
        smsSender.mockSend(phone,code);
    }

    @Recover
    public void recoverSendSms(Exception e){
          log.error("短信发送失败，重试次数已用完，请检查手机号是否正确，或者联系管理员");
    }
}
